﻿' BasecampVB - http://www.tidbitsandtangents.com
' Copyright (c) 2009
' by Rusty S. Swayne
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports TnT.BasecampVB.DAL

Namespace BLL

    Public Class Post
        Inherits BasecampBase

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New( _
            ByVal post_id As Integer, _
            ByVal project_id As Integer, _
            ByVal title As String, _
            ByVal author_id As Integer, _
            ByVal author_name As String, _
            ByVal category_id As Integer, _
            ByVal category_name As String, _
            ByVal body As String, _
            ByVal display_body As String, _
            ByVal display_extended_body As String, _
            ByVal extended_body As String, _
            ByVal milestone_id As Integer, _
            ByVal attachments_count As Integer, _
            ByVal comments_count As Integer, _
            ByVal from_client As Integer, _
            ByVal posted_on As DateTime, _
            ByVal is_private As Boolean, _
            ByVal read_by_person As Integer, _
            ByVal read_by_person_id As Integer, _
            ByVal use_textile As Boolean)

            Me.post_id = post_id
            Me.project_id = project_id
            Me.title = title
            Me.author_id = author_id
            Me.author_name = author_name
            Me.category_id = category_id
            Me.category_name = category_name
            Me.body = body
            Me.display_body = display_body
            Me.display_extended_body = display_extended_body
            Me.extended_body = extended_body
            Me.milestone_id = milestone_id
            Me.attachements_count = attachements_count
            Me.comments_count = comments_count
            Me.from_client = from_client
            Me.posted_on = posted_on
            Me.is_private = is_private
            Me.read_by_person = read_by_person
            Me.read_by_person_id = read_by_person_id
            Me.use_textile = use_textile

        End Sub

        ''' <summary>
        ''' Returns the 25 most recent messages in the given project
        ''' </summary>
        Public Shared Function GetPost(ByVal post_id As Integer) As Post
            Return GetPostFromPostDetails(BasecampProvider.API.GetPost(post_id))
        End Function

        ''' <summary>
        ''' Returns Post from Basecamp API (Basecamp Message)
        ''' </summary>
        Public Shared Function GetPosts(ByVal project_id As Integer) As List(Of Post)
            Return GetPostListFromPostDetailsCollection(BasecampProvider.API.GetPosts(project_id))
        End Function

        ''' <summary>
        ''' Returns Post by category from Basecamp API (Basecamp Message)
        ''' </summary>
        Public Shared Function GetPostsByCategory(ByVal project_id As Integer, ByVal category_id As Integer) As List(Of Post)
            Return GetPostListFromPostDetailsCollection(BasecampProvider.API.GetPostsByCategory(project_id, category_id))
        End Function

        ''' <summary>
        ''' Returns Post of archived posts from Basecamp API (Basecamp Message)
        ''' </summary>
        Public Shared Function GetArchivedPosts(ByVal project_id As Integer) As List(Of Post)
            Return GetPostListFromPostDetailsCollection(BasecampProvider.API.GetArchivedPosts(project_id))
        End Function

        ''' <summary>
        ''' Returns Post of archived posts by category from Basecamp API (Basecamp Message)
        ''' </summary>
        Public Shared Function GetArchivedPostsByCategory(ByVal project_id As Integer, ByVal category_id As Integer) As List(Of Post)
            Return GetPostListFromPostDetailsCollection(BasecampProvider.API.GetArchivedPostsByCategory(project_id, category_id))
        End Function

#Region "Create and Update Posts"

        ''' <summary>
        ''' Creates a Message in Basecamp via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function CreatePost(ByVal project_id As Integer, ByVal title As String, ByVal body As String, Optional ByVal is_private As Boolean = False) As Integer
            Return CreatePost(project_id, title, body, String.Empty, is_private)
        End Function

        ''' <summary>
        ''' Creates a Message in Basecamp via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function CreatePost(ByVal project_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Integer
            Return CreatePost(project_id, 0, title, body, extended_body, is_private)
        End Function

        ''' <summary>
        ''' Creates a Message in Basecamp via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function CreatePost(ByVal project_id As Integer, ByVal category_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Integer
            Return CreatePost(project_id, category_id, 0, title, body, extended_body, is_private)
        End Function

        ''' <summary>
        ''' Creates a Message in Basecamp via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function CreatePost(ByVal project_id As Integer, ByVal category_id As Integer, ByVal milestone_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Integer
            Return CreatePost(project_id, category_id, milestone_id, title, body, extended_body, is_private, Nothing)
        End Function

        ''' <summary>
        ''' Creates a Message in Basecamp via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function CreatePost(ByVal project_id As Integer, ByVal category_id As Integer, ByVal milestone_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, ByVal is_private As Boolean, ByVal notify As List(Of Person)) As Integer
            Dim person_ids As List(Of Integer) = Nothing
            Dim p As Person = Nothing
            If notify IsNot Nothing Then
                person_ids = New List(Of Integer)
                For Each p In notify
                    person_ids.Add(p.person_id)
                Next
            End If
            Return BasecampProvider.API.CreatePost(project_id, category_id, milestone_id, title, body, extended_body, is_private, person_ids)
        End Function

        ''' <summary>
        ''' Update a Basecamp Message via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function UpdatePost(ByVal post_id As Integer, ByVal title As String, ByVal body As String, Optional ByVal is_private As Boolean = False) As Boolean
            Return UpdatePost(post_id, title, body, String.Empty, is_private)
        End Function

        ''' <summary>
        ''' Update a Basecamp Message via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function UpdatePost(ByVal post_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Boolean
            Return UpdatePost(post_id, 0, title, body, extended_body, is_private)
        End Function

        ''' <summary>
        ''' Update a Basecamp Message via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function UpdatePost(ByVal post_id As Integer, ByVal category_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Boolean
            Return UpdatePost(post_id, category_id, 0, title, body, extended_body, is_private)
        End Function

        ''' <summary>
        ''' Update a Basecamp Message via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function UpdatePost(ByVal post_id As Integer, ByVal category_id As Integer, ByVal milestone_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Boolean
            Return UpdatePost(post_id, category_id, milestone_id, title, body, extended_body, is_private, Nothing)
        End Function

        ''' <summary>
        ''' Update a Basecamp Message via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Overloads Shared Function UpdatePost(ByVal post_id As Integer, ByVal category_id As Integer, ByVal milestone_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, ByVal is_private As Boolean, ByVal notify As List(Of Person)) As Boolean
            Dim person_ids As List(Of Integer) = Nothing
            Dim p As Person = Nothing
            If notify IsNot Nothing Then
                person_ids = New List(Of Integer)
                For Each p In notify
                    person_ids.Add(p.person_id)
                Next
            End If
            Return BasecampProvider.API.UpdatePost(post_id, category_id, milestone_id, title, body, extended_body, is_private, person_ids)
        End Function

        ''' <summary>
        ''' Update a Basecamp Message via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Function Update() As Boolean
            Return UpdatePost(Me.post_id, Me.category_id, Me.milestone_id, Me.title, Me.body, Me.extended_body, Me.is_private)
        End Function


#End Region

        ''' <summary>
        ''' Deletes a Message in Basecamp via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Shared Function DeletePost(ByVal post_id As Integer) As Boolean
            Return BasecampProvider.API.DeletePost(post_id)
        End Function

        ''' <summary>
        ''' Deletes a Message in Basecamp via Basecamp API (Basecamp Message)
        ''' </summary>
        Public Function Delete() As Boolean
            Dim ret As Boolean = DeletePost(Me.post_id)
            Me.project_id = 0
            Return ret
        End Function

#Region "Comments"

        ''' <summary>
        ''' Adds a comment to the currently instantiated Post (message)
        ''' </summary>
        Public Function AddComment(ByVal body As String) As Integer
            Return BasecampProvider.API.CreateComment(CommentResource.posts, Me.post_id, body)
        End Function

#End Region


#Region "Custom Properties"

        ''' <summary>
        ''' List of comments associated with currently instantiated Post (message)
        ''' </summary>
        Public ReadOnly Property comments() As List(Of Comment)
            Get
                Return Comment.GetComments(CommentResource.posts, Me.post_id)
            End Get
        End Property

#End Region


#Region "Private Utility Methods"

        ''' <summary>
        ''' Helper Function to create a List(Of Post) from List(Of PostDetails) 
        ''' </summary>
        ''' <remarks>Basecamp message</remarks>
        Private Shared Function GetPostListFromPostDetailsCollection(ByVal recordset As List(Of PostDetails)) As List(Of Post)
            Dim posts As New List(Of Post)
            Dim record As PostDetails = Nothing
            For Each record In recordset
                posts.Add(GetPostFromPostDetails(record))
            Next
            Return posts
        End Function

        ''' <summary>
        ''' Helper Function to create a Post from PostDetails
        ''' </summary>
        ''' <remarks>
        ''' Basecamp message
        ''' </remarks>
        Private Shared Function GetPostFromPostDetails(ByVal record As PostDetails) As Post
            Return New Post(record.post_id, record.project_id, record.title, record.author_id, record.author_name, record.category_id, record.category_name, _
                        record.body, record.display_body, record.display_extended_body, record.extended_body, record.milestone_id, record.attachements_count, _
                        record.comments_count, record.from_client, record.posted_on, record.is_private, record.read_by_person, record.read_by_person_id, record.use_textile)
        End Function

#End Region


#Region "Properties"

        Private _post_id As Integer
        Public Property post_id() As Integer
            Get
                Return _post_id
            End Get
            Set(ByVal value As Integer)
                _post_id = value
            End Set
        End Property

        Private _project_id As Integer
        Public Property project_id() As Integer
            Get
                Return _project_id
            End Get
            Set(ByVal value As Integer)
                _project_id = value
            End Set
        End Property

        Private _title As String
        Public Property title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property


        Private _author_id As Integer
        Public Property author_id() As Integer
            Get
                Return _author_id
            End Get
            Set(ByVal value As Integer)
                _author_id = value
            End Set
        End Property

        Private _author_name As String
        Public Property author_name() As String
            Get
                Return _author_name
            End Get
            Set(ByVal value As String)
                _author_name = value
            End Set
        End Property

        Private _category_id As Integer
        Public Property category_id() As Integer
            Get
                Return _category_id
            End Get
            Set(ByVal value As Integer)
                _category_id = value
            End Set
        End Property

        Private _category_name As String
        Public Property category_name() As String
            Get
                Return _category_name
            End Get
            Set(ByVal value As String)
                _category_name = value
            End Set
        End Property

        Private _body As String
        Public Property body() As String
            Get
                Return _body
            End Get
            Set(ByVal value As String)
                _body = value
            End Set
        End Property

        Private _display_body As String
        Public Property display_body() As String
            Get
                Return _display_body
            End Get
            Set(ByVal value As String)
                _display_body = value
            End Set
        End Property

        Private _display_extended_body As String
        Public Property display_extended_body() As String
            Get
                Return _display_extended_body
            End Get
            Set(ByVal value As String)
                _display_extended_body = value
            End Set
        End Property

        Private _extended_body As String
        Public Property extended_body() As String
            Get
                Return _extended_body
            End Get
            Set(ByVal value As String)
                _extended_body = value
            End Set
        End Property

        Private _milestone_id As Integer
        Public Property milestone_id() As Integer
            Get
                Return _milestone_id
            End Get
            Set(ByVal value As Integer)
                _milestone_id = value
            End Set
        End Property

        Private _attachments_count As Integer
        Public Property attachements_count() As Integer
            Get
                Return _attachments_count
            End Get
            Set(ByVal value As Integer)
                _attachments_count = value
            End Set
        End Property

        Private _comments_count As Integer
        Public Property comments_count() As Integer
            Get
                Return _comments_count
            End Get
            Set(ByVal value As Integer)
                _comments_count = value
            End Set
        End Property


        Private _from_client As Integer
        Public Property from_client() As Integer
            Get
                Return _from_client
            End Get
            Set(ByVal value As Integer)
                _from_client = value
            End Set
        End Property

        Private _posted_on As DateTime
        Public Property posted_on() As DateTime
            Get
                Return _posted_on
            End Get
            Set(ByVal value As DateTime)
                _posted_on = value
            End Set
        End Property

        Private _private As Boolean
        Public Property is_private() As Boolean
            Get
                Return _private
            End Get
            Set(ByVal value As Boolean)
                _private = value
            End Set
        End Property

        Private _read_by_person As Integer
        Public Property read_by_person() As Integer
            Get
                Return _read_by_person
            End Get
            Set(ByVal value As Integer)
                _read_by_person = value
            End Set
        End Property

        Private _read_by_person_id As Integer
        Public Property read_by_person_id() As Integer
            Get
                Return _read_by_person_id
            End Get
            Set(ByVal value As Integer)
                _read_by_person_id = value
            End Set
        End Property

        Private _use_textile As Boolean
        Public Property use_textile() As Boolean
            Get
                Return _use_textile
            End Get
            Set(ByVal value As Boolean)
                _use_textile = value
            End Set
        End Property


#End Region

    End Class

End Namespace
